var module_stopwatch = Class.extend({
	options : null,
	continued : false,
	timerOn : false,
	startTime : null,
	endTime : null,
	currDuration : null,
	hours : 0,
	mins : 0,
	secs : 0,
	dHours : '00',
	dMins : '00',
	dSecs : '00',
	_timer : null,
	_timerTimeout : 10,
	_offsetTime : 0,
	init : function(options) {
		this.options = options;
		this.reset();
		this.renderDisplay();
	},
	start : function() {
		this.timerOn = true;
		if (this.startTime == null) {
			this.startTime = this._getCurrTime();
		} else {
			if (this.endTime != null) {
				this.continued = true;
				this._offSetTime = (this._getCurrTime() - this.endTime) / 1000;
			}
		}

		this.endTime = null;
		this._startTimer();
		return true;
	},
	stop : function() {
		this.timerOn = false;
		this.endTime = this._getCurrTime();
		this.renderDisplay();
		this._stopTimer();
		return true;
	},
	reset : function() {
		this.continued = false;
		this.timerOn = false;
		this.startTime = null;
		this.endTime = null;
		this.currDuration = null;

		this.hours = 0;
		this.mins = 0;
		this.secs = 0;

		this.dHours = '00', this.dMins = '00', this.dSecs = '00', this._offsetTime = 0;

		this._stopTimer();

		this.renderDisplay();
		return true;
	},
	renderDisplay : function() {
		this.dSecs = this.secs.toFixed(2);
		if (this.secs < 10) {
			this.dSecs = '0' + this.secs.toFixed(2);
		}

		this.dMins = this.mins;
		if (this.mins < 10) {
			this.dMins = '0' + this.mins;
		}

		this.dHours = this.hours;
		if (this.hours < 10) {
			this.dHours = '0' + this.hours;
		}

		this.options.hours.html(this.dHours);
		this.options.minutes.html(this.dMins);
		this.options.seconds.html(this.dSecs);
	},

	getState : function() {
		var state = {
			//options: this.options,
			continued : this.continued,
			startTime : this.startTime,
			endTime : this.endTime,
			hours : this.hours,
			mins : this.mins,
			secs : this.secs,
			dHours : this.dHours,
			dMins : this.dMins,
			dSecs : this.dSecs,
			_timerTimeout : this._timerTimeout,
			_offsetTime : this._offsetTime
		};
		return state;
	},

	setState : function(stateObj) {
		this.reset();
		var instance = this;
		for (var key in stateObj) {
			if (stateObj.hasOwnProperty(key)) {
				instance[key] = stateObj[key];
			}
		}
	},
	_getCurrDuration : function() {
		var endTime = this.endTime == null ? this._getCurrTime() : this.endTime
		if (this.startTime != null)
			return (endTime - this.startTime) / 1000;
		return 0.00;
	},
	_startTimer : function() {
		this._runClock();
		this.renderDisplay();
		return true;
	},

	_runClock : function() {
		if (this.timerOn == true) {
			if (this.continued == true) {
				this.currDuration = this._getCurrDuration() - this._offSetTime;
			} else {
				this.currDuration = this._getCurrDuration();
			}

			this.secs = this.currDuration;
			if (this.secs > 59) {
				this.secs = 0;
				this.mins += 1;
			}
			if (this.mins == 60) {
				this.mins = 0;
				this.hours += 1;
			}
			var that = this;
			this._timer = window.setTimeout(function() {
				that._runClock();
				that.renderDisplay();
			}, this._timerTimeout);
		}
	},

	_stopTimer : function() {
		if (this._timer != null) {
			clearTimeout(this._timer);
			this._timer = null;
			return true;
		}
		return false;
	},

	_getCurrTime : function() {
		return new Date().getTime();
	}
}); 